package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.request.HttpRequestUtil;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Liang Jianlin
 * @date 2024-06-01 19:45
 */

public class ProjectRegisterController {
    @FXML
    public RadioButton isPublic;
    @FXML
    public ComboBox typeCombobox;
    public TextField teamName;
    Map<String,Long> teacherMap = new HashMap<>();
    public ComboBox teacherCombobox;
    public TextField name;
    public TextArea desc;
    public Label output;
    public Button selectPerson;
    public Button confirm;
    private JSONArray memberList;
    public void initialize(){
        HttpResponse<JsonNode> response = HttpUtil.get(Globalvar.baseUrl+"/api/teachers")
                .header("Accept", "application/json")
                .asJson();
        JsonNode responseBody = response.getBody();
        JSONArray jsonArray1 = responseBody.getArray();
        for (int i = 0; i < jsonArray1.length(); i++) {
            JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
            String name= jsonObject1.getString("name");
            Long teacherId= Long.valueOf(jsonObject1.getString("studentId"));
            teacherCombobox.getItems().add(name+teacherId);
            teacherMap.put(name+teacherId, teacherId);
        }
        selectPerson.setOnAction(e->{
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/student-select.fxml"));
            Parent root;
            try {
                root = fxmlLoader.load();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            StudentSelectController SSC = fxmlLoader.getController();
            SSC.confirm.setOnAction(e1->{
                memberList = SSC.getSelectedStudentList();
                SSC.output.setText("选择成功");
            });
            Stage stage = new Stage();
            Scene scene = new Scene(root, 500.0, 400.0);
            stage.setTitle("选择成员");
            stage.setScene(scene);
            stage.show();
            stage.setOnCloseRequest(event -> {
                HttpRequestUtil.close();
            });
        });
        confirm.setOnAction(e->{
            JSONObject data = new JSONObject();
            data.put("id",Globalvar.specificId);
            data.put("type",typeCombobox.getValue());
            data.put("projectName",name.getText());
            data.put("teamName",teamName.getText());
            data.put("description",desc.getText());
            data.put("isPublic",isPublic.isSelected());
            data.put("teacherId",teacherMap.get(teacherCombobox.getValue()));
            data.put("memberList",memberList);
            //发请求
        });
    }

}
